package com.fgchen.study.user.server;

import com.fgchen.study.user.service.LoginService;
import com.fgchen.study.user.service.UserService;
import com.fgchen.study.user.service.impl.LoginServiceImpl;
import com.fgchen.study.user.service.impl.UserServiceImpl;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.protocol.TJSONProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;

/**
 * Created by fgchen on 2017/7/18.
 */
public class MySelectorServer {

    public static void main(String[] args) throws Exception {
        TNonblockingServerTransport transport = new TNonblockingServerSocket(8888);

        TMultiplexedProcessor processor = new TMultiplexedProcessor();
        processor.registerProcessor("LoginService", new LoginService.Processor<>(new LoginServiceImpl()));
        processor.registerProcessor("UserService", new UserService.Processor<>(new UserServiceImpl()));

        TThreadedSelectorServer server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args(transport)
                .processor(processor)
                .protocolFactory(new TJSONProtocol.Factory())
                .selectorThreads(2)
                .workerThreads(6)
        );

        Thread serverThread = new Thread(new RunnableServer(server));
        serverThread.start();
        serverThread.join();
    }

    public static class RunnableServer implements Runnable {

        private TServer server;

        public RunnableServer(TServer server) {
            this.server = server;
        }

        @Override
        public void run() {
            this.server.serve();
        }
    }

}
